create PROC [dbo].[SupplementaryInsurance_CheckDuplicate]
    @Name NVARCHAR(50),
    @Code VARCHAR(3),
    @IsInsert SMALLINT
AS
DECLARE @Result VARCHAR(100);
IF @IsInsert = 1
BEGIN
    IF EXISTS (SELECT Code FROM SupplementaryInsurance WHERE Code = @Code)
        SET @Result = 'DuplicateCode';
    ELSE IF EXISTS (SELECT [Name] FROM SupplementaryInsurance WHERE Name = @Name)
        SET @Result = 'DuplicateName';
    ELSE
        SET @Result = 'NoDuplicate';
    SELECT Result = @Result;
END;
ELSE IF @IsInsert = 0
BEGIN
    IF EXISTS
    (
        SELECT [Name]
        FROM SupplementaryInsurance
        WHERE [Name] = @Name
              AND Code <> @Code
    )
        SET @Result = 'DuplicateName';
    ELSE
        SET @Result = 'NoDuplicate';
    SELECT Result = @Result;
END;
ELSE
BEGIN
    IF EXISTS
    (
        SELECT [Name]
        FROM SupplementaryInsurance
        WHERE [Name] = @Name
              AND Code = @Code
    )
        SET @Result = 'CanDelete';
    ELSE
        SET @Result = 'CanNotDelete';
    SELECT Result = @Result;
END;